package com.jiazhong.example.project.bookstore.front.listener;
// 失败 退款消费者

import com.alibaba.fastjson.JSONArray;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.jiazhong.example.project.bookstore.commons.bean.Alipay;
import com.jiazhong.example.project.bookstore.commons.bean.RocketMQOrder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

@Slf4j
//@RocketMQMessageListener(consumerGroup = "order_fail_2", topic = "Order", selectorExpression = "error", messageModel = MessageModel.BROADCASTING)
@Component
public class OrderFailRefundAmountListener implements RocketMQListener<String> {

    @SneakyThrows
    @Override
    public void onMessage(String s) {
        RocketMQOrder rocketMQOrder = JSONArray.parseObject(s, RocketMQOrder.class);

        Alipay alipay = new Alipay();
        alipay.setOut_trade_no(rocketMQOrder.getOrderId());
        alipay.setRefund_amount(rocketMQOrder.getTotalPrice());
        log.info("开始执行退款操作：{}", alipay);
        AlipayClient client = rocketMQOrder.getAlipayClient();
        // 产生退款对象
        AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest();
        String json = JSONArray.toJSONString(alipay);
        refundRequest.setBizContent(json);
        AlipayTradeRefundResponse refundResponse = client.execute(refundRequest);
        if (refundResponse.isSuccess()) {
            log.info("退款成功");
        }
        log.info("退款失败");
    }
}
